﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VinEmiCalculator
{
    class EMILogic
    {
        /*
        E = P×r×(1 + r)n/((1 + r)n - 1)
        E is EMI
        where P is Priniple Loan Amount
        r is rate of interest calualted in monthly basis it should be = Rate of Annual interest/12/100
        if its 10% annual ,then its 10/12/100=0.00833
        n is tenture in number of months
        */
        public EMILogic(double principal, Double annualInterest, int tenure)
        { Principal = principal; AnnualInterest = annualInterest; Tenure = tenure; }
        public double Principal { get; set; }
        public double AnnualInterest { get; set; }
        public double MonthlyInterest { get; set; }
        public int Tenure { get; set; }

        public Double EMI { get; set; }


        public void CalculateEMI()
        {
            if (Principal == 0)
            { EMI = 0; return; }
            if (AnnualInterest == 0)
            { EMI = 0; return; }
            if (Tenure == 0)
            { EMI = 0; return; }

            MonthlyInterest = AnnualInterest/12/100;

            double part0 = Principal * MonthlyInterest;
            double part1 = Math.Pow((1D + MonthlyInterest), Convert.ToDouble(Tenure*12));  // (1+r)pwr n
            double part2 = part1 - 1D;

            EMI = part0 * part1 / part2; 
        }

    }
}
